home *** CD-ROM | disk | FTP | other *** search
- (* $Id$ *)
-
- (* $Log$
- *)
-
- MODULE ScanDrv;
-
- FROM Scanner IMPORT BeginScanner, GetToken, EofToken, CloseScanner;
-
- BEGIN
- BeginScanner;
- WHILE GetToken () # EofToken DO END;
- CloseScanner;
- END ScanDrv.
- (* $Id$ *)
-
- (* $Log$
- *)
-
- DEFINITION MODULE Scanner;
-
- TYPE tAttribute = INTEGER;
- PROCEDURE ErrorAttribute (Token: CARDINAL; VAR Attribute: tAttribute);
-
- CONST
- EofToken = 0;
-
- VAR
- Line ,
- Column : CARDINAL;
- Attribute : tAttribute;
- ScanTabName : ARRAY [0 .. 127] OF CHAR;
-
- PROCEDURE BeginScanner;
- PROCEDURE GetToken (): INTEGER;
- PROCEDURE CloseScanner;
-
- END Scanner.
- (* $Id$ *)
-
- (* $Log$
- *)
-
- IMPLEMENTATION MODULE Scanner;
-
- IMPORT SYSTEM, Checks, SystemIO, IO, Source, Strings;
-
- VAR level : CARDINAL;
-
- PROCEDURE ErrorAttribute (Token: CARDINAL; VAR Attribute: tAttribute);
- BEGIN
- END ErrorAttribute;
-
-
- CONST
- yyDNoState = 0;
- yyTokenLngMax = 256;
- yyChBufferSize = 2048;
- yyFirstCh = 0C;
- yyLastCh = 200C;
- yyEolCh = 12C;
- yyEobCh = 200C;
- yyDStateCount = 218;
- yyTableSize = 833;
- yyEobState = 5;
- yyDefaultState = 218;
- STD = 1;
- comment = 3;
-
- TYPE
- yyTableElmt = SHORTCARD;
- yyStateRange = yyTableElmt [0 .. yyDStateCount];
- yyTableRange = yyTableElmt [0 .. yyTableSize];
- yyCombType = RECORD
- Check ,
- Next : yyStateRange ;
- END;
- yyCombTypePtr = POINTER TO yyCombType;
- yyStStackRange = SHORTCARD [0 .. yyTokenLngMax];
- yyChBufferRange = SHORTCARD [0 .. yyTokenLngMax + yyChBufferSize + 2];
- yyChRange = [yyFirstCh .. yyLastCh];
-
- VAR
- yyTableFile : SystemIO.tFile;
- yyElmtSize : yyTableElmt;
- yyBasePtr : ARRAY yyStateRange OF LONGCARD ;
- yyDefault : ARRAY yyStateRange OF yyStateRange ;
- yyComb : ARRAY yyTableRange OF yyCombType ;
- yyTokenLength : CARDINAL;
- yyLineCount : CARDINAL;
- yyStateStack : ARRAY yyStStackRange OF yyStateRange ;
- yyStartState : yyStateRange;
- yyPreviousStart : yyStateRange;
- yyChBuffer : ARRAY yyChBufferRange OF CHAR ;
- yyChBufferIndex : yyChBufferRange;
- yyBytesRead : INTEGER;
- yyToLower : ARRAY yyChRange OF yyChRange ;
- yyToUpper : ARRAY yyChRange OF yyChRange ;
- yyCh : CHAR;
-
- PROCEDURE GetToken (): INTEGER;
- VAR
- yyState : yyStateRange;
- yyTablePtr : yyCombTypePtr;
- yyRestartFlag : BOOLEAN;
- yyi : SHORTINT;
- BEGIN
- LOOP
- Line := yyLineCount; (* initialize *)
- INC (Column, yyTokenLength);
- yyState := yyStartState;
- yyTokenLength := 0;
-
- (* ASSERT yyChBuffer [yyChBufferIndex] = first character *)
-
- LOOP (* eventually restart after sentinel *)
- LOOP (* execute as many state transitions as possible *)
- (* determine next state *)
- yyTablePtr := yyCombTypePtr (yyBasePtr [yyState] +
- ORD (yyChBuffer [yyChBufferIndex]) * SYSTEM.TSIZE (yyCombType));
- IF yyTablePtr^.Check # yyState THEN
- yyState := yyDefault [yyState];
- IF yyState = yyDNoState THEN EXIT; END;
- ELSE
- yyState := yyTablePtr^.Next;
- INC (yyTokenLength);
- yyStateStack [yyTokenLength] := yyState; (* push state *)
- INC (yyChBufferIndex); (* get next character *)
- END;
- END;
-
- LOOP (* search for last final state *)
- CASE yyStateStack [yyTokenLength] OF
- |216:
- INC (level); yyStart (comment);
- yyRestartFlag := FALSE; EXIT;
- |215:
- DEC (level); IF level = 0 THEN yyStart (STD); END;
- yyRestartFlag := FALSE; EXIT;
- |6,17,31,214,217:
-
- yyRestartFlag := FALSE; EXIT;
- |8,20:
- RETURN 1;
- yyRestartFlag := FALSE; EXIT;
- |13:
- DEC (yyChBufferIndex, 2);
- DEC (yyTokenLength, 2);
- RETURN 1;
- yyRestartFlag := FALSE; EXIT;
- |14:
- RETURN 2;
- yyRestartFlag := FALSE; EXIT;
- |15:
- RETURN 3;
- yyRestartFlag := FALSE; EXIT;
- |12:
- RETURN 4;
- yyRestartFlag := FALSE; EXIT;
- |10,21,22:
- RETURN 5;
- yyRestartFlag := FALSE; EXIT;
- |16:
- RETURN 6;
- yyRestartFlag := FALSE; EXIT;
- |213:
- RETURN 7;
- yyRestartFlag := FALSE; EXIT;
- |212:
- RETURN 8;
- yyRestartFlag := FALSE; EXIT;
- |211:
- RETURN 9;
- yyRestartFlag := FALSE; EXIT;
- |210:
- RETURN 10;
- yyRestartFlag := FALSE; EXIT;
- |209:
- RETURN 11;
- yyRestartFlag := FALSE; EXIT;
- |208:
- RETURN 12;
- yyRestartFlag := FALSE; EXIT;
- |207:
- RETURN 13;
- yyRestartFlag := FALSE; EXIT;
- |206:
- RETURN 14;
- yyRestartFlag := FALSE; EXIT;
- |204:
- RETURN 15;
- yyRestartFlag := FALSE; EXIT;
- |205:
- RETURN 16;
- yyRestartFlag := FALSE; EXIT;
- |203:
- RETURN 17;
- yyRestartFlag := FALSE; EXIT;
- |201:
- RETURN 18;
- yyRestartFlag := FALSE; EXIT;
- |202:
- RETURN 19;
- yyRestartFlag := FALSE; EXIT;
- |200:
- RETURN 20;
- yyRestartFlag := FALSE; EXIT;
- |197:
- RETURN 21;
- yyRestartFlag := FALSE; EXIT;
- |199:
- RETURN 22;
- yyRestartFlag := FALSE; EXIT;
- |198:
- RETURN 23;
- yyRestartFlag := FALSE; EXIT;
- |196:
- RETURN 24;
- yyRestartFlag := FALSE; EXIT;
- |194:
- RETURN 25;
- yyRestartFlag := FALSE; EXIT;
- |195:
- RETURN 26;
- yyRestartFlag := FALSE; EXIT;
- |193:
- RETURN 27;
- yyRestartFlag := FALSE; EXIT;
- |192:
- RETURN 28;
- yyRestartFlag := FALSE; EXIT;
- |191:
- RETURN 29;
- yyRestartFlag := FALSE; EXIT;
- |190:
- RETURN 30;
- yyRestartFlag := FALSE; EXIT;
- |189:
- RETURN 31;
- yyRestartFlag := FALSE; EXIT;
- |188:
- RETURN 32;
- yyRestartFlag := FALSE; EXIT;
- |187:
- RETURN 33;
- yyRestartFlag := FALSE; EXIT;
- |186:
- RETURN 34;
- yyRestartFlag := FALSE; EXIT;
- |184:
- RETURN 35;
- yyRestartFlag := FALSE; EXIT;
- |179:
- RETURN 36;
- yyRestartFlag := FALSE; EXIT;
- |175:
- RETURN 37;
- yyRestartFlag := FALSE; EXIT;
- |173:
- RETURN 38;
- yyRestartFlag := FALSE; EXIT;
- |170:
- RETURN 39;
- yyRestartFlag := FALSE; EXIT;
- |165:
- RETURN 40;
- yyRestartFlag := FALSE; EXIT;
- |156:
- RETURN 41;
- yyRestartFlag := FALSE; EXIT;
- |154:
- RETURN 42;
- yyRestartFlag := FALSE; EXIT;
- |152:
- RETURN 43;
- yyRestartFlag := FALSE; EXIT;
- |151:
- RETURN 44;
- yyRestartFlag := FALSE; EXIT;
- |147:
- RETURN 45;
- yyRestartFlag := FALSE; EXIT;
- |145:
- RETURN 46;
- yyRestartFlag := FALSE; EXIT;
- |143:
- RETURN 47;
- yyRestartFlag := FALSE; EXIT;
- |137:
- RETURN 48;
- yyRestartFlag := FALSE; EXIT;
- |135:
- RETURN 49;
- yyRestartFlag := FALSE; EXIT;
- |131:
- RETURN 50;
- yyRestartFlag := FALSE; EXIT;
- |130:
- RETURN 51;
- yyRestartFlag := FALSE; EXIT;
- |119:
- RETURN 52;
- yyRestartFlag := FALSE; EXIT;
- |114:
- RETURN 53;
- yyRestartFlag := FALSE; EXIT;
- |112:
- RETURN 54;
- yyRestartFlag := FALSE; EXIT;
- |105:
- RETURN 55;
- yyRestartFlag := FALSE; EXIT;
- |108:
- RETURN 56;
- yyRestartFlag := FALSE; EXIT;
- |102:
- RETURN 57;
- yyRestartFlag := FALSE; EXIT;
- |99:
- RETURN 58;
- yyRestartFlag := FALSE; EXIT;
- |98:
- RETURN 59;
- yyRestartFlag := FALSE; EXIT;
- |96:
- RETURN 60;
- yyRestartFlag := FALSE; EXIT;
- |90:
- RETURN 61;
- yyRestartFlag := FALSE; EXIT;
- |81:
- RETURN 62;
- yyRestartFlag := FALSE; EXIT;
- |72:
- RETURN 63;
- yyRestartFlag := FALSE; EXIT;
- |68:
- RETURN 64;
- yyRestartFlag := FALSE; EXIT;
- |64:
- RETURN 65;
- yyRestartFlag := FALSE; EXIT;
- |58:
- RETURN 66;
- yyRestartFlag := FALSE; EXIT;
- |55:
- RETURN 67;
- yyRestartFlag := FALSE; EXIT;
- |52:
- RETURN 68;
- yyRestartFlag := FALSE; EXIT;
- |51:
- RETURN 69;
- yyRestartFlag := FALSE; EXIT;
- |47:
- RETURN 70;
- yyRestartFlag := FALSE; EXIT;
- |42:
- RETURN 71;
- yyRestartFlag := FALSE; EXIT;
- |39:
- RETURN 72;
- yyRestartFlag := FALSE; EXIT;
- |35:
- RETURN 73;
- yyRestartFlag := FALSE; EXIT;
- |9,32,33,34,36,37,38,40,41,43,44,45,46,48,49,50,53,54,56,57,59,60,61,62,63,65,66,67,69,70,71,73,74,75,76,77,78,79,80,82,83,84,85,86,87,88,89,91,92,93,94,95,97,100,101,103,104,106,107,109,110,111,113,115,116,117,118,120,121,122,123,124,125,126,127,128,129,132,133,134,136,138,139,140,141,142,144,146,148,149,150,153,155,157,158,159,160,161,162,163,164,166,167,168,169,171,172,174,176,177,178,180,181,182,183,185:
- RETURN 74;
- yyRestartFlag := FALSE; EXIT;
- |7,18,19:
- IO.WriteS (IO.StdOutput, "illegal character: ");
- yyEcho;
- IO.WriteNl (IO.StdOutput);
- yyRestartFlag := FALSE; EXIT;
- |30:
- (* BlankAction *)
- WHILE yyChBuffer [yyChBufferIndex] = ' ' DO
- INC (yyChBufferIndex);
- INC (yyTokenLength);
- END;
- INC (Column, yyTokenLength);
- yyState := yyStartState;
- yyTokenLength := 0;
- yyRestartFlag := TRUE; EXIT;
- yyRestartFlag := FALSE; EXIT;
- |29:
- (* TabAction *)
- INC (Column, 7 - (Column - 1) MOD 8);
- yyRestartFlag := FALSE; EXIT;
- |28:
- (* EolAction *)
- INC (yyLineCount);
- Column := 0;
- yyRestartFlag := FALSE; EXIT;
- |0,1,2,3,4,11,23,24,25,26,27:
- (* non final states *)
- DEC (yyChBufferIndex); (* return character *)
- DEC (yyTokenLength) (* pop state *)
-
- | yyDefaultState :
- IO.WriteC (IO.StdOutput, yyChBuffer [yyChBufferIndex]);
- INC (yyChBufferIndex);
- yyRestartFlag := FALSE; EXIT;
-
- | yyEobState :
-
- IF yyBytesRead = 0 THEN (* end of file reached *)
- IF Source.CloseSource () THEN
- yyTokenLength := 0;
- RETURN EofToken;
- ELSE
- yyGetChBuffer;
- yyRestartFlag := FALSE;
- EXIT;
- END;
- ELSE (* read buffer *)
- DEC (yyChBufferIndex);
- DEC (yyTokenLength);
- IF yyTokenLength = 0 THEN
- yyState := yyStartState;
- ELSE
- yyState := yyStateStack [yyTokenLength];
- END;
-
- (* copy initial part of token in front of input buffer *)
-
- FOR yyi := - INTEGER (yyTokenLength) TO -1 DO
- yyChBuffer [yyTokenLngMax + 1 + yyi] :=
- yyChBuffer [SHORTINT (yyChBufferIndex) + yyi];
- END;
- yyGetChBuffer;
- yyRestartFlag := TRUE;
- EXIT;
- END;
-
- ELSE
- yyAnalyzeError;
- END;
- END;
- IF yyRestartFlag THEN ELSE EXIT; END;
- END;
- END;
- END GetToken;
-
- PROCEDURE yyGetChBuffer;
- BEGIN
- yyChBufferIndex := yyTokenLngMax + 1;
- yyBytesRead := Source.GetLine (SYSTEM.ADR (yyChBuffer [yyChBufferIndex]), yyChBufferSize);
- IF yyBytesRead < 0 THEN
- yyBytesRead := 0;
- END;
- yyChBuffer [yyBytesRead + yyTokenLngMax + 1] := yyEobCh;
- yyChBuffer [yyBytesRead + yyTokenLngMax + 2] := 0C;
- END yyGetChBuffer;
-
- PROCEDURE GetWord (VAR Word: Strings.tString);
- VAR
- i : CARDINAL;
- WordStart : yyChBufferRange;
- BEGIN
- WordStart := yyChBufferIndex - yyTokenLength - 1;
- FOR i := 1 TO yyTokenLength DO
- Word.Chars [i] := yyChBuffer [WordStart + i];
- END;
- Word.Length := yyTokenLength;
- END GetWord;
-
- PROCEDURE GetLower (VAR Word: Strings.tString);
- VAR
- i : CARDINAL;
- WordStart : yyChBufferRange;
- BEGIN
- WordStart := yyChBufferIndex - yyTokenLength - 1;
- FOR i := 1 TO yyTokenLength DO
- Word.Chars [i] := yyToLower [yyChBuffer [WordStart + i]];
- END;
- Word.Length := yyTokenLength;
- END GetLower;
-
- PROCEDURE GetUpper (VAR Word: Strings.tString);
- VAR
- i : CARDINAL;
- WordStart : yyChBufferRange;
- BEGIN
- WordStart := yyChBufferIndex - yyTokenLength - 1;
- FOR i := 1 TO yyTokenLength DO
- Word.Chars [i] := yyToUpper [yyChBuffer [WordStart + i]];
- END;
- Word.Length := yyTokenLength;
- END GetUpper;
-
- PROCEDURE yyStart (State: yyStateRange);
- BEGIN
- yyPreviousStart := yyStartState;
- yyStartState := State;
- END yyStart;
-
- PROCEDURE yyPrevious;
- BEGIN
- yyStartState := yyPreviousStart;
- END yyPrevious;
-
- PROCEDURE yyEcho;
- VAR Word : Strings.tString;
- BEGIN
- GetWord (Word);
- Strings.WriteS (IO.StdOutput, Word);
- END yyEcho;
-
- PROCEDURE yyLess (n: CARDINAL);
- BEGIN
- DEC (yyChBufferIndex, yyTokenLength - n);
- yyTokenLength := n;
- END yyLess;
-
- PROCEDURE yyTab;
- BEGIN
- INC (Column, 7 - (Column - 1) MOD 8);
- END yyTab;
-
- PROCEDURE yyTab2 (a, b: CARDINAL);
- BEGIN
- INC (Column, a);
- INC (Column, 7 - (Column - 1) MOD 8);
- INC (Column, b);
- END yyTab2;
-
- PROCEDURE yyEol (pColumn: CARDINAL);
- BEGIN
- INC (yyLineCount);
- Column := pColumn;
- END yyEol;
-
- PROCEDURE BeginScanner;
- BEGIN
- IF yyStartState = 0 THEN
- yyGetTables;
- END;
- yyStartState := STD;
- yyTokenLength := 0;
- yyLineCount := 1;
- Column := 1;
- yyStateStack [0] := yyDefaultState;
- yyChBuffer [yyTokenLngMax] := yyEolCh;
- yyGetChBuffer;
- level := 0;
- END BeginScanner;
-
- PROCEDURE CloseScanner;
- BEGIN
- END CloseScanner;
-
- PROCEDURE yyGetTables;
- VAR
- i : yyStateRange;
- yyBase : ARRAY yyStateRange OF yyTableRange ;
- BEGIN
- yyTableFile := SystemIO.OpenInput (ScanTabName);
- Checks.ErrorCheck ("yyGetTables.OpenInput", yyTableFile);
- IF (yyGetTable (SYSTEM.ADR (yyBase )) DIV yyElmtSize - 1
- # yyDStateCount) OR
- (yyGetTable (SYSTEM.ADR (yyDefault)) DIV yyElmtSize - 1
- # yyDStateCount) OR
- (yyGetTable (SYSTEM.ADR (yyComb )) DIV SYSTEM.TSIZE (yyCombType) - 1
- # yyTableSize) THEN
- IO.WriteS (IO.StdError, "Scanner: table mismatch");
- IO.WriteNl (IO.StdError);
- IO.CloseIO;
- HALT;
- END;
- SystemIO.Close (yyTableFile);
-
- FOR i := 0 TO yyDStateCount DO
- yyBasePtr [i] := LONGCARD (SYSTEM.ADR (yyComb [yyBase [i]]));
- END;
- END yyGetTables;
-
- PROCEDURE yyGetTable (Address: SYSTEM.ADDRESS): CARDINAL;
- VAR
- N : INTEGER;
- Length : yyTableElmt;
- BEGIN
- N := SystemIO.Read (yyTableFile, SYSTEM.ADR (Length), yyElmtSize);
- Checks.ErrorCheck ("yyGetTable.Read1", N);
- N := SystemIO.Read (yyTableFile, Address, Length);
- Checks.ErrorCheck ("yyGetTable.Read2", N);
- RETURN Length;
- END yyGetTable;
-
- PROCEDURE yyAnalyzeError;
- BEGIN
- IF yyStartState = 0 THEN
- IO.WriteS (IO.StdError, "Scanner: initialization missing");
- ELSIF yyTokenLength >= yyTokenLngMax THEN
- IO.WriteS (IO.StdError, "Scanner: token too long");
- ELSE
- IO.WriteS (IO.StdError, "Scanner: internal error");
- END;
- IO.WriteNl (IO.StdError);
- IO.CloseIO;
- HALT;
- END yyAnalyzeError;
-
- BEGIN
- yyElmtSize := SYSTEM.TSIZE (yyTableElmt);
- yyStartState := 0;
- ScanTabName := "ScanTab";
-
- FOR yyCh := yyFirstCh TO yyLastCh DO
- yyToLower [yyCh] := yyCh;
- yyToUpper [yyCh] := yyCh;
- END;
- FOR yyCh := 'A' TO 'Z' DO
- yyToLower [yyCh] := CHR (ORD (yyCh) - ORD ('A') + ORD ('a'));
- END;
- FOR yyCh := 'a' TO 'z' DO
- yyToUpper [yyCh] := CHR (ORD (yyCh) - ORD ('a') + ORD ('A'));
- END;
- END Scanner.
- (* $Id$ *)
-
- (* $Log$
- *)
-
- DEFINITION MODULE Source;
-
- FROM SYSTEM IMPORT ADDRESS;
-
- PROCEDURE BeginSource ();
-
- (*
- BeginSource is called before scanning starts.
- It can be used to open files etc.
- *)
-
- PROCEDURE GetLine (Buffer: ADDRESS; Size: CARDINAL): INTEGER;
-
- (*
- GetLine is called to fill a buffer starting at address 'Buffer'
- with a block of maximal 'Size' characters. Lines are terminated
- by newline characters (ASCII = 0A). GetLine returns the number
- of characters transferred. Reasonable block sizes are between 128
- and 2048 or the length of a line. Smaller block sizes -
- especially block size 1 - will drastically slow down the scanner.
- *)
-
- PROCEDURE CloseSource (): BOOLEAN;
-
- (*
- CloseSource is called at end of file resp. input.
- It can be used to close files or to redirect input.
-
- if CloseSource returns true : scanner stops
- if CloseSource returns false : scanner continues
- *)
-
- END Source.
- (* $Id$ *)
-
- (* $Log$
- *)
-
- IMPLEMENTATION MODULE Source;
-
- FROM SystemIO IMPORT StdInput, Read;
- FROM SYSTEM IMPORT ADDRESS;
-
- PROCEDURE BeginSource ();
- BEGIN
- END BeginSource;
-
- PROCEDURE GetLine (Buffer: ADDRESS; Size: CARDINAL): INTEGER;
- BEGIN
- RETURN Read (StdInput, Buffer, Size);
- END GetLine;
-
- PROCEDURE CloseSource (): BOOLEAN;
- BEGIN
- RETURN TRUE;
- END CloseSource;
-
- BEGIN
- BeginSource;
- END Source.
-